package animationEngine;

import java.awt.Dimension;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import algorithms.BoyerMooreTestAlgorithm;
import algorithms.KMPTestAlgorithm;

@SuppressWarnings("serial")
/**
 * BorderTable is used to represent the window of extra information that may be required by
 * a pre-processing stage of some algorithms (in our case, Boyer Moore and KMP).
 * 
 * @author Andrei Palade
 * @author David McBrierty
 * @author Garry Sharp
 * @author Hristo Georgiev
 * @author John Dennis
 * @author Marco Sarconi
 * @version 1.0.1
 */
public class PreProcessingFrame extends JInternalFrame{
	
	public final static int BORDER_TABLE = 0;
	public final static int LAST_OCCURRENCE_TABLE = 1;
	
	public StringBox tableBox;
	public JPanel tableBoxPanel;
	
	public StringBox indexBox;
	public JPanel indexBoxPanel;
	
	public double updateLocation;
	
	private JPanel listPane;
	
	private String indexString = "";
	private String tableString = "";
	
	private JLabel tableLabel;
	private JPanel tableLabelPanel;
	
	public Dimension screenSize;
	
	/** The Constant borderTableExplanation. */
	private final static String borderTableExplanation = "<html>A <u>substring</u> of string <b><i>s</i></b> is a sequence of consecutive characters of <b><i>s</i></b>.<br />" +
	"A <u>prefix</u> of <b><i>s</i></b> is a substring that begins at position <b><i>0</i></b>.<br />" +
	"A <u>suffix</u> of <b><i>s</i></b> is a substring that ends at position <b><i>n-1</i></b>.<br />" +
	"A <u>border</u> of a string <b><i>s</i></b> is a substring that is a prefix and a suffix and cannot be the string itself.<br /><br />" +
	"The KMP algorithm requires the <u>border table</u> of the string pattern, " +
	"so it can compute the new position of the string.<br /><br />" +
	"<u>Border table b:</u> array which has the same size as the string<br />" +
	"b[j] = the length of the longest border of <b><i>s[0..j-1]</i></b><br />" +
	"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= <b><i>max { k | s[0..k-1] = s[j-k..j-1] }</i></b></html>";
	
	public PreProcessingFrame(Dimension screenSize, String pattern, int typeOfTable){
		//super("Borfhder Table", true, false, false, true);
		this.screenSize = screenSize;
		
		if (typeOfTable == BORDER_TABLE){
			//super("Border Table", true, false, false, true);
			this.setTitle("Border Table");
			this.setLocation(0, (int) (0.20 * screenSize.height));
			this.setSize((int) (0.34 * screenSize.width), (int) (0.53 * screenSize.height));
			
			this.listPane = new JPanel();
			int [] t = new int[pattern.length()];
			KMPTestAlgorithm.setUp(pattern, t);
			//System.out.println("Border table: pattern is " + pattern);
			
			for (int i = 0; i < t.length; i++) {
				//System.out.println("Setting indexString")
				indexString += pattern.charAt(i);
				tableString += String.valueOf(t[i]);
			}
			//System.out.println("Creating tableBox with string =" + tableString);
			//System.out.println("Creating tableBox with string =" + indexString);
			tableBox = new StringBox(tableString);
			indexBox = new StringBox(indexString);
			
			/*String borderTableExplanation = "<html>A <u>substring</u> of string <b><i>s</i></b> is a sequence of consecutive characters of <b><i>s</i></b>.<br />" +
					"A <u>prefix</u> of <b><i>s</i></b> is a substring that begins at position <b><i>0</i></b>.<br />" +
					"A <u>suffix</u> of <b><i>s</i></b> is a substring that ends at position <b><i>n-1</i></b>.<br />" +
					"A <u>border</u> of a string <b><i>s</i></b> is a substring that is a prefix and a suffix and cannot be the string itself.<br /><br />" +
					"The KMP algorithm requires the <u>border table</u> of the string pattern, " +
					"so it can compute the new position of the string.<br /><br />" +
					"<u>Border table b:</u> array which has the same size as the string<br />" +
					"b[j] = the length of the longest border of <b><i>s[0..j-1]</i></b><br />" +
					"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= <b><i>max { k | s[0..k-1] = s[j-k..j-1] }</i></b></html>";*/			
			tableLabel = new JLabel(borderTableExplanation, JLabel.LEFT);
			tableLabel.setPreferredSize(new Dimension((int) (0.29 * screenSize.width), (int) (0.38 * screenSize.height)));
			listPane.setOpaque(true);
			indexBoxPanel = new JPanel();
			indexBoxPanel.setLocation(0, 0);
			indexBox.setPreferredSize(new Dimension((int) (0.29 * screenSize.width), (int) (0.04 * screenSize.height)));
			indexBoxPanel.setSize(indexBox.getSize().width, indexBox.getSize().height);
			indexBox.setOpaque(false);
			//indexBoxPanel.setOpaque(true);
			indexBoxPanel.add(indexBox);								
			listPane.add(indexBoxPanel);
			
			tableBoxPanel = new JPanel();
			tableBoxPanel.setLocation(0, 36);
			tableBox.setPreferredSize(new Dimension((int) (0.29 * screenSize.width), (int) (0.04 * screenSize.height)));
			tableBoxPanel.setSize(tableBox.getSize().width, tableBox.getSize().height);
			tableBox.setOpaque(false);
			//tableBoxPanel.setOpaque(true);
			tableBoxPanel.add(tableBox);
			listPane.add(tableBoxPanel);
			
			tableLabelPanel = new JPanel();
			tableLabelPanel.setLocation(0, 72);
			tableLabelPanel.setSize(tableLabel.getSize().width, tableLabel.getSize().height);
			tableLabelPanel.add(tableLabel);
		    listPane.add(tableLabelPanel);
			
			this.add(listPane);
		}
		else if (typeOfTable == LAST_OCCURRENCE_TABLE){
			//BUILD LAST OCCURRENCE TABLE
			this.setTitle("Last Occurrence in Pattern");
			this.setLocation(0, (int) (0.20 * screenSize.height));
			this.setSize((int) (0.34 * screenSize.width), (int) (0.53 * screenSize.height));
			this.setVisible(true);
			this.listPane = new JPanel();
			int [] t = new int[BoyerMooreTestAlgorithm.NUM_ASCII_SYMBOLS];
			//System.out.println("Last occurrence in pattern: pattern is " + pattern);
			BoyerMooreTestAlgorithm.setUp(pattern, t, pattern.length());
			
			for (int i = 0; i < pattern.length(); i++) {
				indexString += pattern.charAt(i);
				tableString += t[pattern.charAt(i)] == -1 ? "-" : String.valueOf(t[pattern.charAt(i)]);
				//System.out.format("tableString: [%s]\n", tableString);
			}
			
			//BoyerMooreTestAlgorithm.printTable(t);
			
			tableBox = new StringBox(tableString);
			indexBox = new StringBox(indexString);
			
			String lastOccurenceTableExplanation = "<html>The string is scanned right-to-left text character involved in a mismatch is used to decide next comparison involves pre-processing the string to record the position of the last occurrence of each character c in the alphabet therefore the alphabet must be fixed in advance of the search.<br /><br />" +
					"Last occurrence position of character <b><i>c</i></b> in the string s equals <b><i>max{k | s[k]=c }</i></b> if such a <b><i>k</i></b> exists and -1 otherwise. " +
					"We assume an array <b><i>p</i></b> indexed by characters the characters range over the underlying alphabet of the text <b><i>p[c]</i></b> records the position in the string of the last occurrence of char <b><i>c</i></b> if the character <b><i>c</i></b> is absent from the string <b><i>s</i></b>, then let <b><i>p[c]</i></b> = -1<br /><br />" +
					"Assume ASCII character set (128 characters).</html>";			
			tableLabel = new JLabel(lastOccurenceTableExplanation, JLabel.LEFT);
			tableLabel.setPreferredSize(new Dimension((int) (0.29 * screenSize.width), (int) (0.38 * screenSize.height)));
				
			indexBoxPanel = new JPanel();
			indexBoxPanel.setLocation(0, 0);
			indexBox.setPreferredSize(new Dimension((int) (0.29 * screenSize.width), (int) (0.04 * screenSize.height)));
			indexBoxPanel.setSize(indexBox.getSize().width, indexBox.getSize().height);
			indexBox.setOpaque(false);
			indexBoxPanel.add(indexBox);								
			listPane.add(indexBoxPanel);
			
			tableBoxPanel = new JPanel();
			tableBoxPanel.setLocation(0, 36);
			tableBox.setPreferredSize(new Dimension((int) (0.29 * screenSize.width), (int) (0.04 * screenSize.height)));
			tableBoxPanel.setSize(tableBox.getSize().width, tableBox.getSize().height);
			tableBox.setOpaque(false);
			tableBoxPanel.add(tableBox);
			listPane.add(tableBoxPanel);
			
			tableLabelPanel = new JPanel();
			tableLabelPanel.setLocation(0, 72);
			tableLabelPanel.setSize(tableLabel.getSize().width, tableLabel.getSize().height);
			tableLabelPanel.add(tableLabel);
	        listPane.add(tableLabelPanel);
			
			this.add(listPane);
		}
		
		this.setVisible(true);
		//this.indexBox.highlightChar(1, Color.BLUE);
	}
	
	public PreProcessingFrame(){
		this.setTitle("Border Table");
		//this.setLocation((int) (0.33 * screenSize.width), (int) (0.20 * screenSize.height));
		//this.setSize((int) (0.34 * screenSize.width), (int) (0.53 * screenSize.height));
		//this.setVisible(true);
	}
	public StringBox getTableBox() {
		return tableBox;
	}
	


	public void setTableBox(StringBox tableBox) {
		this.tableBox = tableBox;
	}

	public StringBox getIndexBox() {
		return indexBox;
	}

	public void setIndexBox(StringBox indexBox) {
		this.indexBox = indexBox;
	}	

	//public void highlightIndex()
}
